﻿Imports OMANE.Common.UI
Imports OMANE.Common
Imports System.ComponentModel
Imports System.Xml.Serialization
Imports System.Windows.Input
Imports PTIC_Util
Imports System.Windows.Data
Imports System.Windows
Imports System.Windows.Controls
Imports OMANE.Common.Model.Interfaces
Imports Actuariat
Imports Microsoft.Practices.Prism.Commands

Namespace ViewModels
    Public Class ViewModelTarif
        Inherits ViewModelCommand
        Implements IViewModelTabSection

        Private WithEvents m_moteurEcheancier As IMoteur_Echeancier

        Public Sub New(ByVal IMoteurEcheancier As IMoteur_Echeancier,
                       ByVal ITablesMortaliteFactory As Actuariat.ITablesMortaliteFactory,
                       ByVal ITablesMaintienINCINVFactory As Actuariat.ITablesMaintienINCINVFactory,
                       ByVal DisplayName As String,
                       ByVal Command As ICommand)
            MyBase.New(DisplayName, Command)
            m_moteurEcheancier = IMoteurEcheancier
            m_TablesMortaliteFactory = ITablesMortaliteFactory
            m_TablesMaintienINCINVFactory = ITablesMaintienINCINVFactory
            Title = "TARIFICATION"
            'If _moteurEcheancier.TableMort_Str = "" Then
            '    _moteurEcheancier.TableMort_Str = TablesMortalite.Item(0)("strNomFichier")
            'End If
            'If _moteurEcheancier.TableMaintienINCAPINVAL_Str = "" Then
            '    _moteurEcheancier.TableMaintienINCAPINVAL_Str = TablesMaintienINCINV.Item(0)("strNomFichier")
            'End If
        End Sub
        Sub New()
        End Sub

        Public Property IsClosable As Boolean Implements IViewModelTabSection.IsClosable


#Region "IViewModelTabSection"
        Public Property Icone As String Implements Common.UI.IViewModelTabSection.Icone
        Public Property Title As String Implements Common.UI.IViewModelTabSection.Title
#End Region

#Region "Paramètres en entrée"
        Public ReadOnly Property MoteurEcheancier As IMoteur_Echeancier
            Get
                Return m_moteurEcheancier
            End Get
        End Property
        Public Property TxNomAnnuel As Double
            Get
                Return m_moteurEcheancier.Input.TxNomMens * 12
                'Return Math.Round((_model.Univers.listeTraitements.TraitMoteurEcheancier.TxNomPer) * 12, 4)
            End Get
            Set(ByVal value As Double)
                Dim txNomMens As Double = Actuariat.Mensualise_tx(CDbl(value), Actuariat.TypeMensualisation.TauxProportionel)
                m_moteurEcheancier.Input.TxNomMens = txNomMens
            End Set
        End Property
        Public Property TxTechAnnuel As Double
            Get
                Return (1 + m_moteurEcheancier.TxTechMens) ^ 12 - 1
            End Get
            Set(ByVal value As Double)
                Dim txTechMens As Double = Actuariat.Mensualise_tx(value, Actuariat.TypeMensualisation.TauxEquivalent)
                m_moteurEcheancier.TxTechMens = txTechMens
            End Set
        End Property
        Public Property TxRAAnnuel As Double
            Get
                Return m_moteurEcheancier.TxRembAntiMens * 12
            End Get
            Set(ByVal value As Double)
                Dim txRAMens As Double = Actuariat.Mensualise_tx(value, Actuariat.TypeMensualisation.TauxProportionel)
                m_moteurEcheancier.TxRembAntiMens = txRAMens
            End Set
        End Property
        Public Property TarifDCAn As Double
            Get
                Return m_moteurEcheancier.TarifDCMens * 12
            End Get
            Set(ByVal value As Double)
                m_moteurEcheancier.TarifDCMens = Actuariat.Mensualise_tx(value, Actuariat.TypeMensualisation.TauxProportionel)
            End Set
        End Property
        Public Property TarifINCINVAn As Double
            Get
                Return m_moteurEcheancier.TarifINCINVMens * 12
            End Get
            Set(ByVal value As Double)
                m_moteurEcheancier.TarifINCINVMens = Actuariat.Mensualise_tx(value, Actuariat.TypeMensualisation.TauxProportionel)
            End Set
        End Property

#Region "Table de maintien en INCAP / INVAL"
        Private m_TablesMaintienINCINVFactory As ITablesMaintienINCINVFactory
        Public ReadOnly Property TablesMaintienINCINV As List(Of String)
            Get
                Return m_TablesMaintienINCINVFactory.NomsDisponibles
            End Get
        End Property

        Public Property TableMaintienINCINV_Selected As String
            Get
                'Vérifier que le nom est bien compris dans la liste des tables de la factory
                Return m_moteurEcheancier.TableMaintienINCAPINVAL.Nom
            End Get
            Set(ByVal value As String)
                If value <> TableMaintienINCINV_Selected Then
                    m_moteurEcheancier.TableMaintienINCAPINVAL = m_TablesMaintienINCINVFactory.Item(value)
                End If
            End Set
        End Property
#End Region

#Region "Table de mortalité"
        Private m_TablesMortaliteFactory As ITablesMortaliteFactory
        Public ReadOnly Property TablesMortalite As List(Of String)
            Get
                Return m_TablesMortaliteFactory.NomsDisponibles
            End Get
        End Property

        Public Property TablesMortalite_Selected As String
            Get
                Return m_moteurEcheancier.TableMort.Nom
            End Get
            Set(ByVal value As String)
                If value <> TablesMortalite_Selected Then
                    m_moteurEcheancier.TableMort = m_TablesMortaliteFactory.Item(value)
                End If
            End Set
        End Property
#End Region

#End Region

#Region "Affichage Echéancier Annuel / Mensuel et Mensualité"
        Public ReadOnly Property EcheancierMensuel As DataTable
            Get
                Return m_moteurEcheancier.Output.ProjectionMensuelle
            End Get
        End Property
        Public ReadOnly Property EcheancierMensuelIsVisible As Visibility
            Get
                If PasProjection = "Mensuelle" Then
                    If m_moteurEcheancier.EtatCalcul = ValueEtatCalcul.A_JOUR Then
                        Return Visibility.Visible
                    Else
                        Return Visibility.Hidden
                    End If
                Else
                    Return Visibility.Hidden
                End If
            End Get
        End Property
        Public ReadOnly Property EcheancierAnnuel As DataTable
            Get
                Return m_moteurEcheancier.Output.ProjectionAnnuelle
            End Get
        End Property
        Public ReadOnly Property EcheancierAnnuelIsVisible As Visibility
            Get
                If PasProjection = "Annuelle" Then
                    If m_moteurEcheancier.EtatCalcul = ValueEtatCalcul.A_JOUR Then
                        Return Visibility.Visible
                    Else
                        Return Visibility.Hidden
                    End If
                Else
                    Return Visibility.Hidden
                End If
            End Get
        End Property
        Public ReadOnly Property Mensualite As Double
            Get
                If m_moteurEcheancier.EtatCalcul = ValueEtatCalcul.A_JOUR Then
                    Return m_moteurEcheancier.Output.EcheanceMensuelle
                Else
                    Return 0
                End If
            End Get
        End Property
        Public ReadOnly Property MensualiteIsVisible As Visibility
            Get
                Return IIf(EcheancierAnnuelIsVisible = Visibility.Hidden And EcheancierMensuelIsVisible = Visibility.Hidden, Visibility.Hidden, Visibility.Visible)
            End Get
        End Property
        Public ReadOnly Property MessageIsVisible As Visibility
            Get
                Return IIf(EcheancierAnnuelIsVisible = Visibility.Hidden And EcheancierMensuelIsVisible = Visibility.Hidden, Visibility.Visible, Visibility.Hidden)
            End Get
        End Property
#End Region

#Region "Pas de la projection"
        Private _PasProjection As String = "Mensuelle"
        Public Property PasProjection As String
            Get
                Return _PasProjection
            End Get
            Set(ByVal value As String)
                If value <> _PasProjection Then
                    OnPropertyChanging("EcheancierAnnuelIsVisible")
                    OnPropertyChanging("EcheancierMensuelIsVisible")
                    OnPropertyChanging("MessageIsVisible")
                    OnPropertyChanging("MensualiteIsVisible")
                    OnPropertyChanging("PasProjection")
                    _PasProjection = value
                    OnPropertyChanged("PasProjection")
                    OnPropertyChanged("MessageIsVisible")
                    OnPropertyChanged("EcheancierAnnuelIsVisible")
                    OnPropertyChanged("EcheancierMensuelIsVisible")
                    OnPropertyChanged("MensualiteIsVisible")
                End If
            End Set
        End Property
        Private _cmdChangePasProjection As DelegateCommand
        Public ReadOnly Property CmdChangePasProjection As ICommand
            Get
                If _cmdChangePasProjection Is Nothing Then
                    _cmdChangePasProjection = New DelegateCommand(AddressOf ChangePasProjection)
                End If
                Return _cmdChangePasProjection
            End Get
        End Property
        Public Sub ChangePasProjection()
            If PasProjection = "Mensuelle" Then
                PasProjection = "Annuelle"
            Else
                PasProjection = "Mensuelle"
            End If
        End Sub
#End Region

        Public ReadOnly Property CalculerIsVisible As Visibility
            Get
                If m_moteurEcheancier.Recalcul = TypeRecalcul.SurOrdre Then Return Visibility.Visible Else Return Visibility.Hidden
            End Get
        End Property
        Private _cmdCalculer As ICommand
        Public ReadOnly Property cmdCalculer As ICommand
            Get
                If _cmdCalculer Is Nothing Then
                    _cmdCalculer = New DelegateCommand(AddressOf CalculerScenCentral)
                End If
                Return _cmdCalculer
            End Get
        End Property
        Public Sub CalculerScenCentral()
            m_moteurEcheancier.Calculer()
        End Sub

        Public ReadOnly Property Recalcul As String
            Get
                Return IIf(m_moteurEcheancier.Recalcul = TypeRecalcul.SurOrdre, "Man.", "Auto")
            End Get
        End Property
        Public ReadOnly Property RecalculColumnSpan As Integer
            Get
                Return IIf(m_moteurEcheancier.Recalcul = TypeRecalcul.SurOrdre, 1, 2)
            End Get
        End Property
        Private _cmdTypeRecalcul As DelegateCommand
        Public ReadOnly Property cmdTypeRecalcul As ICommand
            Get
                If _cmdTypeRecalcul Is Nothing Then
                    _cmdTypeRecalcul = New DelegateCommand(AddressOf changeTypeRecalcul)
                End If
                Return _cmdTypeRecalcul
            End Get
        End Property
        Public Sub changeTypeRecalcul()
            If m_moteurEcheancier.Recalcul = TypeRecalcul.SurOrdre Then m_moteurEcheancier.Recalcul = TypeRecalcul.Automatique Else m_moteurEcheancier.Recalcul = TypeRecalcul.SurOrdre
            OnPropertyChanged("RecalculColumnSpan")
            OnPropertyChanged("Recalcul")
            OnPropertyChanged("CalculerIsVisible")
        End Sub

        Private _cmdParamSelectionMedicale As DelegateCommand
        Public ReadOnly Property cmdSelectionMedicale As ICommand
            Get
                If _cmdParamSelectionMedicale Is Nothing Then
                    _cmdParamSelectionMedicale = New DelegateCommand(AddressOf ParamSelectionMedicale)
                End If
                Return _cmdParamSelectionMedicale
            End Get
        End Property
        Public Sub ParamSelectionMedicale()
            'Dim correspAVisualiser As Correspondance = correspondance
            Dim VMSelectionMedicale As New ViewModelParamSelectionMedicale(m_moteurEcheancier,
                                                                           "Paramétrage de la sélection médicale",
                                                                           Nothing)
            Dim ui = ModuleTauxGaranti._app.GetService(Of IUIService)()
            ui.ShowMessage(VMSelectionMedicale)
        End Sub

        Public Sub Moteur_Echeancier_Calculated(ByVal sender As Object, ByVal e As EventArgs) Handles m_moteurEcheancier.After_Calculer
            OnPropertyChanged("EcheancierMensuel")
            OnPropertyChanged("EcheancierAnnuel")
            OnPropertyChanged("EcheancierMensuelIsVisible")
            OnPropertyChanged("EcheancierAnnuelIsVisible")
            OnPropertyChanged("MessageIsVisible")
            OnPropertyChanged("MensualiteIsVisible")
            OnPropertyChanged("Mensualite")
        End Sub
        Public Sub Moteur_Echeancier_EtatCalculDataTableChanged(ByVal sender As Object, ByVal e As EventArgs) Handles m_moteurEcheancier.EtatCalcul_Changed
            If m_moteurEcheancier.Recalcul <> TypeRecalcul.Automatique Then
                OnPropertyChanged("EcheancierMensuelIsVisible")
                OnPropertyChanged("EcheancierAnnuelIsVisible")
                OnPropertyChanged("MessageIsVisible")
                OnPropertyChanged("MensualiteIsVisible")
            End If
        End Sub

    End Class
End Namespace